package org.benjamin.lending.controller;

import org.benjamin.lending.domain.entity.BorrowTender;
import org.benjamin.lending.form.TenderLogIndex;
import org.benjamin.lending.service.BorrowService;
import org.benjamin.lending.utils.CommonsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by xywen on 2017/6/23.
 */
@Controller
@RequestMapping("tender")
public class TenderController {

    @Autowired
    private BorrowService borrowService;

    @RequestMapping(value = "index")
    public String index(TenderLogIndex tenderLogIndex, ModelMap modelMap){
        String username = CommonsUtils.getSessionUser();
        tenderLogIndex.setUsername(username);
        Page<BorrowTender> tenderPage = this.borrowService.findUserTenderLogPage(tenderLogIndex);
        modelMap.addAttribute("tenderPage",tenderPage);
        return "tender.index";
    }

    @ResponseBody
    @RequestMapping(value = "contractDemand/{borrowId}")
    public byte[] contractDemand(@PathVariable("borrowId") Integer borrowId){
        String username = CommonsUtils.getSessionUser();
        return this.borrowService.contractDemand(borrowId,username,true);
    }
}
